package com.kanshu.ksgb.fastread.doudou.common.business.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.ReaderInputParams;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wcy on 2017\6\22 0022.
 */
public class BookUtils {


    /**
     * 获取书的阅读进度
     * @param order
     * @param chapter_count
     * @return
     */
    public static String getReadProgress(String order, String chapter_count) {
        String s;
        int orders = 0;
        int chapterCount = 0;
        if (!TextUtils.isEmpty(order)) {
            orders = Integer.parseInt(order);
        }
        if (!TextUtils.isEmpty(chapter_count)) {
            chapterCount = Integer.parseInt(chapter_count);
        }
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        if (chapterCount != 0 && orders != 0){
            if (orders > chapterCount) {
                orders = chapterCount;
            }
            float percent = (float) orders * 100 / chapterCount;
            s = decimalFormat.format(percent) + "%";
        }else {
            s = "0%";
        }
        return s;
    }

    /**
     * 格式化后台服务器时间 后台服务器默认是unix时间
     * @param time
     * @param format
     * @return
     */
    public static String formatServerTime(long time, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        time *= 1000;
        Date date = new Date(time);
        return formatter.format(date);
    }
    /**
     * 跳转方法
     * @param context
     * @param link_type
     * @param url
     */
    public static void processJump(Context context, int link_type, String url) {
        switch (link_type) {
            case Constants.JUMP_TYPE_READER:
                ReaderInputParams params = new ReaderInputParams();
                params.book_id = url;
                ReaderJumpConfig.startReaderActivity(context, params);
                //BookReaderActivity.actionStart(context, url);
                break;
            case Constants.JUMP_TYPE_H5:
                ARouterUtils.toActivity(ARouterConfig.COMMON_H5, "url", url);
                break;
            case Constants.JUMP_TYPE_NONE:
                // do nothing
                break;
            case Constants.JUMP_TYPE_BOOK_DETAIL:
                // 跳转书详情
                ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", url);
                break;
            case Constants.JUMP_TYPE_SIGN:
                break;
            default:
                break;
        }
    }




    /**
     * 格式化（超过1万 显示XX.xx万）
     * @param num
     * @return
     */
    public static String formatNum(String num) {
        if (TextUtils.isEmpty(num)) {
            return "0";
        }
        DecimalFormat decimalFormat = new DecimalFormat("#0");
        long data = Long.parseLong(num);
        if (data > 10000) {
            float d = (float) data / 10000;
            return decimalFormat.format(d) + "万";
        } else {
            return num;
        }
    }

    public static String getBookLabel(BookInfo bookInfo) {
        if (bookInfo == null) {
            return "";
        }
        if (bookInfo.category_id_2 != null) {
            return bookInfo.category_id_2.name;
        }
        if (bookInfo.category_id_1 != null) {
            return bookInfo.category_id_1.name;
        }
        return "";
    }

    public static String getBookStatus(BookInfo bookInfo) {
        if (bookInfo == null) {
            return "连载中";
        }
        if ("0".equals(bookInfo.writing_process)) {
            return "连载中";
        } else {
            return "已完结";
        }
    }

    public static void setStatusBg(SuperTextView tv, BookInfo bookInfo) {
        tv.setStrokeColor(Color.TRANSPARENT);
        tv.setTextColor(Color.TRANSPARENT);
        if ("0".equals(bookInfo.writing_process)) {
            tv.setBackgroundResource(R.mipmap.ic_book_status_starting);
        } else {
            tv.setBackgroundResource(R.mipmap.ic_book_status_finished);
        }
    }

    public static void setStatusImgBg(ImageView tv, BookInfo bookInfo) {
        if ("0".equals(bookInfo.writing_process)) {
            tv.setImageResource(R.mipmap.ic_book_status_starting);
        } else {
            tv.setImageResource(R.mipmap.ic_book_status_finished);
        }
    }

    public static String getRankingNum(int pos) {
        if (pos < 9) {
            return "0" + ++pos;
        } else {
            return "" + ++pos;
        }
    }


    public static String getPercentStr(int cur, int total) {
        String s;
        DecimalFormat decimalFormat = new DecimalFormat("#0");
        if (total != 0 && cur != 0){
            if (cur > total) {
                cur = total;
            }
            float percent = (float) cur * 100 / total;
            s = decimalFormat.format(percent) + "%";
        }else {
            s = "0%";
        }
        return s;
    }

    /**
     * 从后台拉取颜色控制文字颜色
     *
     * @param color
     * @return
     */
    public static void parseColor(TextView tv, String color) {
        if (TextUtils.isEmpty(color)) {
            return;
        }
        if (tv != null && (color.length() == 7 || color.length() == 9) && color.startsWith("#")) {
            tv.setTextColor(Color.parseColor(color));
        }
    }


}
